package com.shop.dao;

import java.util.List;


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.shop.entity.Business;
import com.shop.entity.User;
import com.shop.util.HibernateUtil;

public class BusinessDaoImpl {
	
	 public Business addBusiness(Business business) {
	    	Session session = null; 
	    	session = HibernateUtil.openSession();
	    	Transaction tx =session.beginTransaction();
	    	session.save(business);
	        tx.commit();
	        session.close();
	        return business;
	    } 
	
//	判断用户是否存在
    public boolean isExist(Business business){
    	Session session = null;
    	session = HibernateUtil.openSession();
    	String hql = "from Business where username =  ? and password = ?";
		Query query = session.createQuery(hql).setProperties(business);
		query.setParameter(0,business.getUsername());
		query.setParameter(1,business.getPassword());
		List<Business> list=query.list();
		business = null;
		if(list.size()>0){
			business=list.get(0);
			return true;
		}else{
			return false;
		}
	}
//	查询数据库中的用户
    public List<Business> getAllBusiness() {
    	Session session = null;
    	session = HibernateUtil.openSession();
    	String hql = "from Business";
    	Query query= session.createQuery(hql);
		List<Business> ulist=query.list();
        return query.list();
    }
}
